//
//  SpecialPageViewController.m
//  MoGuRadio
//
//  Created by 李响 on 15/6/3.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

// ViewControllers
#import "qiangXingXiuGai.h"
#import "CollectPageViewController.h"

// 自定义类
#import "Special.h"
#import "Detail.h"
#import "BaseModel.h"
#import "DLBView.h"
#import "MushRoom.h"
#import "AllDetail.h"

// 第三方
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "UIButton+InitButton.h"
#import "MJRefresh.h"

// 自定义cell
#import "SpecialTableViewCell.h"

#import "MBProgressHUD.h"
#import "DataBaseHandler.h"
// 表视图的重用池宏
#define DATATABLEVIEW_REUSE @"DataTableViewReuseIdentifier"

@interface qiangXingXiuGai () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, MBProgressHUDDelegate> {
    
    MBProgressHUD *hud;
//    NSInteger num;
}

// 接收数据的数组
@property (nonatomic, retain) NSMutableArray *specialArr;
@property (nonatomic, retain) NSDictionary *specialDic;
@property (nonatomic, retain) NSMutableArray *detailArr;
// 用来存放上拉加载出来的数据
@property (nonatomic, retain) NSMutableArray *mainArr;

@property (nonatomic, retain) UIScrollView *mainScrollView;// 主滚动
@property (nonatomic, retain) UIView *mainView;// 上半部分的UIView
@property (nonatomic, retain) UIView *sectionView;// 下半部分表视图分区UIView
@property (nonatomic, retain) UITableView *dataTableView;// 显示数据的表视图

// 专题页面数据
@property (nonatomic, retain) UIImageView *picLeftImage;// 左边主播图片
@property (nonatomic, retain) UILabel *hostNameLabel;// 主播名字
@property (nonatomic, retain) UIImageView *foucsImage;// 关注的小图标
@property (nonatomic, retain) UIImageView *listenImage;// 收听小图标
@property (nonatomic, retain) UILabel *foucsLabel;// 关注人数
@property (nonatomic, retain) UILabel *listenLabel;// 收听人数

@property (nonatomic, retain) UIButton *shareButton;// 分享按钮
@property (nonatomic, retain) UIButton *foucsButton;// 关注按钮
@property (nonatomic, retain) UIButton *listenAllButton; // 收听全部按钮
@property (nonatomic, retain) UILabel *descLabel;// 电台介绍

@property (nonatomic, assign, getter=isLabelExtend) BOOL labelExtend;

@end

@implementation qiangXingXiuGai


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mainArr = [NSMutableArray array];
    [self setUpSubViews];
    [self.mainScrollView addFooterWithTarget:self action:@selector(DetailHandler)];
    [self.mainScrollView footerBeginRefreshing];
    [self MBProgressHUD];
}

- (void)setUpSubViews
{
    self.mainScrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainScrollView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0];
    self.mainScrollView.contentSize = CGSizeMake(0, (VIEW_HEIGHT / 6.5) * 10 + 20);
    NSLog(@"============count = %ld", self.detailArr.count);
    self.mainScrollView.bounces = YES;
    self.mainScrollView.delegate = self;
    [self.view addSubview:self.mainScrollView];
    [_mainScrollView release];
    
    self.dataTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, VIEW_HEIGHT / 2.5, VIEW_WIDTH, (VIEW_HEIGHT / 6.5) * 20 + 30) style:UITableViewStylePlain];
    self.dataTableView.delegate = self;
    self.dataTableView.dataSource = self;
    self.dataTableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
    self.dataTableView.scrollEnabled = NO;
    [self.mainScrollView addSubview:self.dataTableView];
    [_dataTableView release];
    
    [self.dataTableView registerClass:[SpecialTableViewCell class] forCellReuseIdentifier:DATATABLEVIEW_REUSE];
    
    
    self.mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT / 2.5)];
    self.mainView.backgroundColor = [UIColor whiteColor];
    [self.mainScrollView addSubview:self.mainView];
    [_mainView release];
    
    [self setUp];
}


// 创建专题页面上半部分的全部控件
- (void)setUp
{
    
    self.picLeftImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, VIEW_WIDTH / 4, VIEW_HEIGHT / 7)];
    self.picLeftImage.backgroundColor = [UIColor blueColor];
    NSString *picImage = self.pic;
    NSURL *url = [NSURL URLWithString:picImage];
    [self.picLeftImage setImageWithURL:url];
    [self.mainView addSubview:self.picLeftImage];
    [_picLeftImage release];
    
    
    self.hostNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 3, VIEW_HEIGHT / 38, VIEW_WIDTH / 2 + 40, VIEW_HEIGHT / 18)];
    self.hostNameLabel.backgroundColor = [UIColor clearColor];
    self.hostNameLabel.numberOfLines = 0;
    self.hostNameLabel.tintColor = [UIColor whiteColor];
    self.hostNameLabel.font = [UIFont boldSystemFontOfSize:18];
    self.hostNameLabel.text = [NSString stringWithFormat:@"主播: %@", self.hostName];
    [self.mainView addSubview:self.hostNameLabel];
    [_hostNameLabel release];
    
    
    self.foucsImage = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 3, VIEW_HEIGHT / 10, VIEW_WIDTH / 30, VIEW_HEIGHT / 50)];
    self.foucsImage.image = [UIImage imageNamed:@"like.png"];
    [self.mainView addSubview:self.foucsImage];
    [_foucsImage release];
    
    self.foucsLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2.6, VIEW_HEIGHT / 10.3, VIEW_WIDTH / 2, VIEW_HEIGHT / 38)];
    self.foucsLabel.text = [BaseModel foucsChangeStringValue:self.foucsNum];
    self.foucsLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.mainView addSubview:self.foucsLabel];
    [_foucsLabel release];

    
    self.listenImage = [[UIImageView alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 3, VIEW_HEIGHT / 7, VIEW_WIDTH / 25, VIEW_HEIGHT / 45)];
    self.listenImage.image = [UIImage imageNamed:@"listen.png"];
    [self.mainView addSubview:self.listenImage];
    [_listenImage release];
    
    
    self.listenLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 2.6, VIEW_HEIGHT / 7.1, VIEW_WIDTH / 2, VIEW_HEIGHT / 38)];
    self.listenLabel.text = [BaseModel listenChangeStringValue:self.listenNum];
    self.listenLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.mainView addSubview:self.listenLabel];
    [_listenLabel release];
    
    
    self.shareButton = [UIButton buttonWithTitle:@"分享" frame:CGRectMake(VIEW_WIDTH / 17, VIEW_HEIGHT / 4.5, VIEW_WIDTH / 4, VIEW_HEIGHT / 20) target:self action:@selector(shareButtonAction:)];
    self.shareButton.backgroundColor = [UIColor colorWithRed:0.11 green:0.56 blue:1.0 alpha:0.8];
    [self.shareButton.layer setMasksToBounds:YES];
    [self.shareButton.layer setCornerRadius:5.0];
    [self.shareButton.layer setBorderWidth:0.12];
    [self.mainView addSubview:self.shareButton];
    
    
    self.foucsButton = [UIButton buttonWithTitle:@"关注" frame:CGRectMake(VIEW_WIDTH / 3 + 17, VIEW_HEIGHT / 4.5, VIEW_WIDTH / 4, VIEW_HEIGHT / 20) target:self action:@selector(foucsButtonAction:)];
    self.foucsButton.backgroundColor = [UIColor colorWithRed:0.11 green:0.56 blue:1.0 alpha:0.8];
    [self.foucsButton.layer setMasksToBounds:YES];
    [self.foucsButton.layer setCornerRadius:5.0];
    [self.foucsButton.layer setBorderWidth:0.12];
    [self.mainView addSubview:self.foucsButton];
    
    self.listenAllButton = [UIButton buttonWithTitle:@"收听全部" frame:CGRectMake(VIEW_WIDTH / 1.2 - 50, VIEW_HEIGHT / 4.5, VIEW_WIDTH / 4, VIEW_HEIGHT / 20) target:self action:@selector(listenAllButtonAction:)];
    self.listenAllButton.backgroundColor = [UIColor colorWithRed:0.11 green:0.56 blue:1.0 alpha:0.8];
    [self.listenAllButton.layer setMasksToBounds:YES];
    [self.listenAllButton.layer setCornerRadius:5.0];
    [self.listenAllButton.layer setBorderWidth:0.12];
    [self.mainView addSubview:self.listenAllButton];
    
    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(VIEW_WIDTH / 15, VIEW_HEIGHT / 3.2, VIEW_WIDTH - 30, VIEW_HEIGHT / 15)];
    self.descLabel.text = [NSString stringWithFormat:@"电台介绍: %@", self.descRadio];
    self.descLabel.userInteractionEnabled = YES;
    self.descLabel.numberOfLines = 0;
    self.descLabel.font = [UIFont boldSystemFontOfSize:13];
    [self.mainView addSubview:self.descLabel];
    [_descLabel release];
    
}

// 数据加载条
- (void)MBProgressHUD
{
    // 加载图MBProgressHUD
    // 方式1: 直接在view上show
    hud = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    hud.delegate = self;
    // 常用设置
    // 小矩形的背景颜色
    hud.color = [UIColor grayColor];
    //显示的文字
    hud.labelText = @"玩命加载中......";
    // 是否有遮罩
    hud.dimBackground = YES;
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeIndeterminate;
}

- (void)shareButtonAction:(UIButton *)button
{
    NSLog(@"分享按钮");
}

- (void)foucsButtonAction:(UIButton *)button
{
    DataBaseHandler *handler = [DataBaseHandler shareInstance];

    MushRoom *mushRoom = [[MushRoom alloc] init];
    mushRoom.rid = self.rid;
    mushRoom.pic = self.pic;
    mushRoom.desLabelText = self.descRadio;
    mushRoom.listenLabelText = self.listenNum;
    mushRoom.focusLabelText = self.foucsNum;
    mushRoom.titleLabelText = self.titleLabel;
    mushRoom.hostNameText = self.hostName;
    
    [handler insertMushRoom:mushRoom];
}

- (void)listenAllButtonAction:(UIButton *)button
{
    NSLog(@"收听全部按钮");
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"节目列表";
}

- (void)reverseButton:(UIButton *)button
{
    NSLog(@"倒序");
}

- (void)shareButton:(UIButton *)button
{
    NSLog(@"分享");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mainArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return VIEW_HEIGHT / 6.5;
}

- (void)DetailHandler
{
    
    NSString *dataStr = [NSString stringWithFormat:@"http://api.kaolafm.com/api/v3.1/radio/playlist?radioid=%@&clockid=&installid=0000WJRD&udid=b7c459c16cb33d45c20bb8fa0be1bb29&sessionid=b7c459c16cb33d45c20bb8fa0be1bb291434371731429&imsi=460036090834220&operator=3&network=1×tamp=1434371918&playid=09dc08759b554aa0cc072c0567bbf81f&sign=f383a93642cd5aa9f1828359a47f2db3&appid=0&channel=A-baidu&version=3.5.3&devicetype=0&resolution=480*854&", self.rid];
    dataStr = [dataStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:dataStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        self.detailArr = [[responseObject objectForKey:@"result"] objectForKey:@"dataList"];
        
        for (NSMutableDictionary *dic in self.detailArr) {
            [self.mainArr addObject:dic];
            [self.dataTableView reloadData];
        }
        // 上拉加载, 改变frame
//        self.mainScrollView.contentSize = CGSizeMake(0, (VIEW_HEIGHT / 6.5) * self.mainArr.count + 295);
        self.dataTableView.frame = CGRectMake(0, 0, VIEW_WIDTH, (VIEW_HEIGHT / 6.01) * self.mainArr.count);
        [self.dataTableView footerEndRefreshing];
        [hud hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"错误信息是==%@", error);
        [hud hide:YES];
        
        // 打印错误信息
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请检查网络" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [self.view addSubview:alertView];
        [alertView show];
        [alertView release];
    }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DATATABLEVIEW_REUSE];
    
    self.title = self.titleLabel;
    
    NSString *title = [[self.mainArr objectAtIndex:indexPath.row] objectForKey:@"audioName"];
    cell.titleLabel.text = [NSString stringWithFormat:@" %@", title];
    cell.listenImageView.image = [UIImage imageNamed:@"listen.png"];
    cell.likeImageView.image = [UIImage imageNamed:@"liked.png"];
    cell.updateTimeImageView.image = [UIImage imageNamed:@"clound.png"];
    
    cell.listenNumLabel.text = [[[self.mainArr objectAtIndex:indexPath.row] objectForKey:@"listenNum"] description];
    
    cell.likedNumLabel.text = [[[self.mainArr objectAtIndex:indexPath.row] objectForKey:@"likedNum"] description];
    
    cell.updateTimeLabel.text = [[self.mainArr objectAtIndex:indexPath.row] objectForKey:@"updateTime"];
    return cell;
}

- (void)dealloc
{
    hud.delegate = nil;
    [_rid release];
    [_titleLabel release];
    [_sectionView release];
    [_likedNum release];
    [_upTime release];
    [_foucsNum release];
    [_listenNum release];
    [_foucsImage release];
    [_foucsLabel release];
    [_listenImage release];
    [_listenLabel release];
    [_descLabel release];
    [_hostName release];
    [_mainView release];
    [_pic release];
    [_detailArr release];
    [_specialArr release];
    [_specialDic release];
    [_mainScrollView release];
    [_dataTableView release];
    [_picLeftImage release];
    [_hostNameLabel release];
    [super dealloc];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
